<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="标题" prop="title">
          <el-input
            v-model="queryParams.title"
            placeholder="请输入标题"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="广告状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="请选择广告状态" clearable>
            <el-option
              v-for="dict in dict.type.ad_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
  
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['maincode:advertisements:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['maincode:advertisements:export']"
          >导出</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
  
      <el-table v-loading="loading" :data="paginatedData" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="账户" align="center" prop="userName" />
        <el-table-column label="用户名" align="center" prop="nickName" ></el-table-column>
        <el-table-column label="用户头像"  prop="imageUrl" >
          <template  slot-scope="scope" width="90">
            <ImagePreview style="width:80px;height:80px;border:none;" :src="scope.row.avatar" />
          </template>
        </el-table-column>
        <el-table-column label="用户电话" align="center" prop="phonenumber" width="180"></el-table-column>
        <el-table-column label="用户邮箱" align="center" prop="email" width="200"/>
        <el-table-column label="创建时间" align="center" prop="createTime" width="180"></el-table-column>
        <el-table-column label="用户备注" align="center" prop="remark" ></el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['maincode:advertisements:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['maincode:advertisements:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="display: flex;flex-direction: row-reverse;margin-top: 10px;">
        <el-pagination background v-model:current-page="currpage" v-model:page-size="pagesize" :page-sizes="[10, 20, 50, 100]" layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange"
        @current-change="handleCurrentChange"></el-pagination>
      </div>

  
      <!-- 添加或修改广告对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="用户名" prop="title">
            <el-input v-model="form.title" placeholder="请输入内容" />
          </el-form-item>
          <el-form-item label="用户昵称" prop="title">
            <el-input v-model="form.title1" placeholder="请输入内容" />
          </el-form-item>
          <el-form-item label="邮箱" prop="title">
            <el-input v-model="form.email" placeholder="请输入内容" />
          </el-form-item>
          <el-form-item label="联系方式" prop="title">
            <el-input v-model="form.phone" placeholder="请输入内容" />
          </el-form-item>
          <el-form-item label="性别" prop="title">
            <el-radio-group v-model="form.sex" size="large">
                <el-radio-button label="男" value="1" />
                <el-radio-button label="女" value="2" />
            </el-radio-group>
          </el-form-item>
          <el-form-item label="用户密码" prop="title">
            <el-input v-model="form.password" placeholder="请输入内容" />
          </el-form-item>
          <el-form-item label="用户头像" prop="imageUrl">
            <image-upload :limit="1" v-model="form.imageUrl"/>
          </el-form-item>
          <el-form-item label="状态" prop="title">
            <el-radio-group v-model="form.status" size="large">
                <el-radio-button label="正常" value="1" />
                <el-radio-button label="禁用" value="2" />
                <el-radio-button label="冻结" value="2" />
            </el-radio-group>
          </el-form-item>
          <el-form-item label="用户余额">
            <el-input-number  :min="0" v-model="form.kucun"/>
          </el-form-item>
          <el-form-item label="权限" prop="title">
            <el-radio-group v-model="form.guanli" size="large">
                <el-radio-button label="管理员" value="1" />
                <el-radio-button label="普通用户" value="2" />
            </el-radio-group>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
  
      <!-- 添加或修改广告对话框 -->
      <el-dialog :title="title" :visible.sync="updata" width="1000px" append-to-body>
        <el-form ref="updataForm" :model="updataForm" :rules="rules" label-width="80px">
          <el-form-item label="用户名" prop="title">
            <el-input v-model="updataForm.title" placeholder="请输入内容" />
          </el-form-item>
          <el-form-item label="用户昵称" prop="title">
            <el-input v-model="updataForm.title1" placeholder="请输入内容" />
          </el-form-item>
          <el-form-item label="邮箱" prop="title">
            <el-input v-model="updataForm.email" placeholder="请输入内容" />
          </el-form-item>
          <el-form-item label="联系方式" prop="title">
            <el-input v-model="updataForm.phone" placeholder="请输入内容" />
          </el-form-item>
          <el-form-item label="性别" prop="title">
            <el-radio-group v-model="updataForm.sex" size="large">
                <el-radio-button label="男" value="1" />
                <el-radio-button label="女" value="2" />
            </el-radio-group>
          </el-form-item>
          <el-form-item label="用户密码" prop="title">
            <el-input v-model="updataForm.password" placeholder="请输入内容" />
          </el-form-item>
          <el-form-item label="用户头像" prop="imageUrl">
            <image-upload :limit="1" v-model="updataForm.imageUrl"/>
          </el-form-item>
          <el-form-item label="状态" prop="title">
            <el-radio-group v-model="updataForm.status" size="large">
                <el-radio-button label="正常" value="1" />
                <el-radio-button label="禁用" value="2" />
                <el-radio-button label="冻结" value="2" />
            </el-radio-group>
          </el-form-item>
          <el-form-item label="用户余额">
            <el-input-number  :min="0" v-model="updataForm.kucun"/>
          </el-form-item>
          <el-form-item label="权限" prop="title">
            <el-radio-group v-model="updataForm.guanli" size="large">
                <el-radio-button label="管理员" value="1" />
                <el-radio-button label="普通用户" value="2" />
            </el-radio-group>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitupdataForm">确 定</el-button>
          <el-button @click="updata = false">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </template>
  
  <script>
  import { getAllpeople,addPeople,deletepeople,updatepeople,getmainText } from '@/api/store/index'
  import dayjs from 'dayjs';
  export default {
    name: "Advertisements",
    dicts: ['ad_status'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 广告表格数据
        advertisementsList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        updata:false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          title: null,
          content: null,
          link: null,
          startTime: null,
          expirationTime: null,
          clicks: null,
          status: null,
          imageUrl: null,
        },
        Idproduct:0,
        // 表单参数
        form: {},
        updataForm:{
          type:''
        },

        // 分页
        pagesize:10,
        currpage:1,


        NewData:{},
        // 表单校验
        rules: {
          title: [
            { required: true, message: "不能为空", trigger: "blur" }
          ],
          content: [
            { required: true, message: "价格不能为空", trigger: "blur" }
          ],
          link: [
            { required: true, message: "库存需要填写", trigger: "blur" }
          ],
          startTime: [
            { required: true, message: "上线时间不能为空", trigger: "blur" }
          ],
          expirationTime: [
            { required: true, message: "过期时间不能为空", trigger: "blur" }
          ],
          status: [
            { required: true, message: "广告状态不能为空", trigger: "change" }
          ],
        },
        
      };
    },
    created() {
      this.getList();
    },
    computed:{
      paginatedData() {
        const start = (this.currpage - 1) * this.pagesize;
        const end = start + this.pagesize;
        console.log(this.advertisementsList);
        return this.advertisementsList.slice(start, end);
      }
    },
    methods: {
      /** 查询广告列表 */
      getList() {
        this.loading = true;
        getAllpeople().then(response => {
          this.advertisementsList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          adId: null,
          title: null,
          content: null,
          link: null,
          startTime: null,
          expirationTime: null,
          clicks: null,
          status: null,
          imageUrl: null,
          delFlag: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          remark: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.adId)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加用户";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.updata = true
        this.Idproduct = row
        // getmainText(row.adId).then(res => {
        //     this.updataForm.clicks = res.data.quantity
        //     this.updataForm.imageUrl = res.data.imgUrl
        //     this.updataForm.kucun = res.data.unitPrice
        //     this.updataForm.title = res.data.title
        // })
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            let id;
            if(this.form.status == '正常'){
              id = '0'
            }else if(this.form.status == '禁用'){
              id = '1'
            }else{
              id = '2'
            }
            const sT = dayjs().format('YYYY-MM-DD HH:mm:ss')
            
            addPeople({
                createBy:'admin',
                createTime:sT,
                remark:'管理员',
                userName:this.form.title,
                nickName:this.form.title1,
                email:this.form.email,
                phonenumber:this.form.phone,
                sex:this.form.sex == '男' ? '1' : '2',
                avatar:this.form.imageUrl,
                password:this.form.password,
                sratus:id,
                money:this.form.kucun,
                admin:this.form.guanli == '管理员'? true:false
            }).then(res => {
                this.$modal.msgSuccess("增加成功");
                this.open = false;
                this.getList();
            })
          }
        });
      },
      submitupdataForm(){
        this.$refs["updataForm"].validate(valid => {
          if (valid) {
            let id;
            if(this.form.status == '正常'){
              id = '0'
            }else if(this.form.status == '禁用'){
              id = '1'
            }else{
              id = '2'
            }
            const sT = dayjs().format('YYYY-MM-DD HH:mm:ss')
            updatepeople({
              createBy:'admin',
              createTime:sT,
              remark:'管理员',
              userId:this.Idproduct.userId,
              userName:this.updataForm.title,
              nickName:this.updataForm.title1,
              email:this.updataForm.email,
              phonenumber:this.updataForm.phone,
              sex:this.updataForm.sex == '男' ? '1' : '2',
              avatar:this.updataForm.imageUrl,
              password:this.updataForm.password,
              sratus:id,
              money:this.updataForm.kucun,
              admin:this.updataForm.guanli == '管理员'? true:false
            }).then(res => {
                this.$modal.msgSuccess("修改成功");
                this.updata = false;
                this.updataForm = {}
                this.getList();
            })
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const adIds = row.userId
        this.$modal.confirm('是否确认删除该订单').then(function() {
          return deletepeople(adIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('maincode/advertisements/export', {
          ...this.queryParams
        }, `advertisements_${new Date().getTime()}.xlsx`)
      },


      // 分页
      handleSizeChange(size){
        this.pagesize = size;
        this.currpage = 1;  // 切换页面大小时重新从第一页开始
      },
      handleCurrentChange(page){
        this.currpage = page
      }
    }
  };
  </script>
  